/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scheduler.view.windows;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;
import scheduler.core.task.VirtualTaskPool;
import scheduler.core.utils.TaskPoolValidationException;
import scheduler.view.dialogs.DialogUtils;

/**
 * FXML Controller class
 *
 * @author Dima
 */
public class VirtualPoolController implements Initializable {
    private static int numOfPriors;
    @FXML
    private TextField priority;
    @FXML
    private TextField size;
    @FXML
    private TextField lambda;
    @FXML
    private TextField nu;
    @FXML
    private Button okButton;
    @FXML
    private Button chancelButton;
    private Tooltip errorTooltip;
    
    private VirtualTaskPool pool = null;
    private boolean edit = false;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        errorTooltip = new Tooltip();
        priority.setText(new Integer(numOfPriors).toString());
    }    
    
    @FXML
    void onOkAction(ActionEvent event) throws IOException {
        if (size.getText().isEmpty()) {
            DialogUtils.showErrorDialog("Порожній розмір");
            return;
        } else if (priority.getText().isEmpty()) {
            DialogUtils.showErrorDialog("Порожній пріорітет");
            return;
        } else if (lambda.getText().isEmpty()) {
            DialogUtils.showErrorDialog("Порожня інтенсивність");
            return;
        } else if (nu.getText().isEmpty()) {
            DialogUtils.showErrorDialog("Порожній середній час");
            return;
        }
        try {
            if (!edit) { 
                pool = new VirtualTaskPool(Integer.parseInt((String)priority.getText()), Integer.parseInt(size.getText()), Double.parseDouble(lambda.getText()), Double.parseDouble(nu.getText()));
            } else {
                pool.setPrior(Integer.parseInt((String)priority.getText()));
                pool.setSize(Integer.parseInt(size.getText()));
                pool.setLambda(Double.parseDouble(lambda.getText()));
                pool.setNu(Double.parseDouble(nu.getText()));
            }
        } catch (NumberFormatException | TaskPoolValidationException ex) {
            DialogUtils.showErrorDialog(ex.getMessage());
            if (!edit) {
                pool = null;
            }
            return;
        } 
        
        ((Stage)okButton.getScene().getWindow()).close();
    }
    
    public void setValues(VirtualTaskPool task, int num) {
        this.pool = task;
        priority.setText(String.valueOf(task.getPrior()));
        size.setText(String.valueOf(task.getSize()));
        lambda.setText(String.valueOf(task.getLambda()));
        nu.setText(String.valueOf(task.getNu()));
        edit = true;
    }
    
    static public void setNextPrior(int num){
        numOfPriors = num;
    }
    
    @FXML
    void onChancelAction(ActionEvent event) {
        ((Stage)okButton.getScene().getWindow()).close();
    }

    public VirtualTaskPool getVirtualPool() {
        return pool;
    }
}
